package model

import (
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"gorm.io/datatypes"
)

// IntelligentForm 智能表单结构体
type IntelligentForm struct {
	global.GVA_MODEL
	Title       string         `json:"title" form:"title" gorm:"column:title;comment:表单标题;size:255;not null;"`
	Description string         `json:"description" form:"description" gorm:"column:description;comment:表单描述;type:text;"`
	Status      int            `json:"status" form:"status" gorm:"column:status;comment:状态：0-草稿，1-发布，2-关闭;default:0;"`
	CreateUser  uint           `json:"createUser" form:"createUser" gorm:"column:create_user;comment:创建者ID;not null;"`
	FormConfig  datatypes.JSON `json:"formConfig" form:"formConfig" gorm:"column:form_config;comment:表单配置信息;" swaggertype:"object"`
	ThemeConfig datatypes.JSON `json:"themeConfig" form:"themeConfig" gorm:"column:theme_config;comment:主题配置信息;" swaggertype:"object"`
	SubmitLimit int            `json:"submitLimit" form:"submitLimit" gorm:"column:submit_limit;comment:提交限制，0为不限制;default:0;"`
	SubmitCount int            `json:"submitCount" form:"submitCount" gorm:"column:submit_count;comment:当前提交数量;default:0;"`
	IsPublic    bool           `json:"isPublic" form:"isPublic" gorm:"column:is_public;comment:是否公开：0-私有，1-公开;default:1;"`
	Password    string         `json:"password" form:"password" gorm:"column:password;comment:访问密码;size:255;"`
	StartTime   *time.Time     `json:"startTime" form:"startTime" gorm:"column:start_time;comment:开始时间;"`
	EndTime     *time.Time     `json:"endTime" form:"endTime" gorm:"column:end_time;comment:结束时间;"`
	FormType    string         `json:"formType" form:"formType" gorm:"column:form_type;comment:表单类型：normal-普通表单，generated-生成表单;size:50;default:normal;"`
}

// TableName 智能表单 IntelligentForm自定义表名 gva_intelligent_forms
func (IntelligentForm) TableName() string {
	return "gva_intelligent_forms"
}

// Form 表单基本信息
type Form struct {
	global.GVA_MODEL
	Title       string         `json:"title" form:"title" gorm:"column:title;comment:表单标题;size:255;not null;"`
	Description string         `json:"description" form:"description" gorm:"column:description;comment:表单描述;type:text;"`
	Status      int            `json:"status" form:"status" gorm:"column:status;comment:状态(0:草稿,1:发布,2:关闭);default:0;"`
	CreateUser  uint           `json:"createUser" form:"createUser" gorm:"column:create_user;comment:创建者ID;"`
	ViewCount   int64          `json:"viewCount" form:"viewCount" gorm:"column:view_count;comment:访问次数;default:0;"`
	SubmitCount int64          `json:"submitCount" form:"submitCount" gorm:"column:submit_count;comment:提交次数;default:0;"`
	Settings    datatypes.JSON `json:"settings" form:"settings" gorm:"column:settings;comment:表单设置;" swaggertype:"object"`
	PublishAt   *time.Time     `json:"publishAt" form:"publishAt" gorm:"column:publish_at;comment:发布时间;"`
	CloseAt     *time.Time     `json:"closeAt" form:"closeAt" gorm:"column:close_at;comment:关闭时间;"`

	// 关联字段
	Fields  []FormField  `json:"fields" gorm:"foreignKey:FormID;references:ID"`
	Submits []FormSubmit `json:"submits" gorm:"foreignKey:FormID;references:ID"`
}

// FormField 表单字段定义
type FormField struct {
	global.GVA_MODEL
	FormID     uint           `json:"formId" form:"formId" gorm:"column:form_id;comment:表单ID;index;"`
	FieldType  string         `json:"fieldType" form:"fieldType" gorm:"column:field_type;comment:字段类型;size:50;not null;"`
	Label      string         `json:"label" form:"label" gorm:"column:label;comment:字段标签;size:255;not null;"`
	Name       string         `json:"name" form:"name" gorm:"column:name;comment:字段名称;size:100;not null;"`
	Required   bool           `json:"required" form:"required" gorm:"column:required;comment:是否必填;default:false;"`
	Sort       int            `json:"sort" form:"sort" gorm:"column:sort;comment:排序序号;default:0;"`
	Config     datatypes.JSON `json:"config" form:"config" gorm:"column:config;comment:字段配置;" swaggertype:"object"`
	Validation datatypes.JSON `json:"validation" form:"validation" gorm:"column:validation;comment:验证规则;" swaggertype:"object"`
}

// FormSubmit 表单提交记录
type FormSubmit struct {
	global.GVA_MODEL
	FormID    uint           `json:"formId" form:"formId" gorm:"column:form_id;comment:表单ID;index;"`
	Data      datatypes.JSON `json:"data" form:"data" gorm:"column:data;comment:提交数据;" swaggertype:"object"`
	IP        string         `json:"ip" form:"ip" gorm:"column:ip;comment:提交IP;size:45;"`
	UserAgent string         `json:"userAgent" form:"userAgent" gorm:"column:user_agent;comment:用户代理;size:500;"`
	SubmitBy  uint           `json:"submitBy" form:"submitBy" gorm:"column:submit_by;comment:提交人ID;default:0;"`
}

// 自定义表名
func (Form) TableName() string {
	return "gva_form"
}

func (FormField) TableName() string {
	return "gva_form_field"
}

func (FormSubmit) TableName() string {
	return "gva_form_submit"
}
